import { _decorator, Component, LabelComponent, Vec3, tween, UITransform, Size, isValid, UIOpacityComponent, SpriteFrame, Node, SpriteComponent } from 'cc';
import { UIBase } from './ui_base';
import { PoolManager } from '../../farmework/pool_manager';
const { ccclass, property } = _decorator;

/**
 * tips节点
 */
@ccclass('UITipsBase')
export class UITipsBase extends UIBase {
    @property(LabelComponent)
    public lbTips!: LabelComponent;

    @property(UIOpacityComponent)
    public UIOpacityBg!: UIOpacityComponent;

    @property(SpriteComponent)
    public spIcon!: SpriteComponent;

    private _originPos: Vec3 = new Vec3(0, 0, 0);//初始位置
    private _targetPos: Vec3 = new Vec3(0, 150, 0);//目标位置

    show (content: string) {
        // if (icon) {
        //     this.spIcon.spriteFrame = icon;
        //     this.spIcon.node.active = true;
        // } else {
        //     this.spIcon.node.active = false;
        // }

        this.node.setPosition(this._originPos);
        this.UIOpacityBg.opacity = 255;
        this.lbTips.string = content;
        let size: Size = this.lbTips.node.getComponent(UITransform)?.contentSize as Size;
        if (!isValid(size)) {//size不存在，自我销毁/回收 
            PoolManager.instance.putNode(this.node);
            return;
        }

        this.node.getComponent(UITransform)?.setContentSize(size.width + 100 < 240 ? 240 : size.width + 100, size.height + 30);

        this.scheduleOnce(() => {
            tween(this.node)
                .by(0.8, { position: this._targetPos }, { easing: "backInOut" })
                .call(() => {
                    PoolManager.instance.putNode(this.node);
                })
                .start();

            tween(this.UIOpacityBg)
                .to(0.4, { opacity: 220 }, { easing: 'backIn' })
                .to(0.4, { opacity: 0 }, { easing: 'backInOut' })
                .start();
        }, 0.8);
    }
}
